Welcome.
In this lecture, we're going to explore a fundamental programming concept known as variables.
A variable is a container or a labeled storage box that can hold different values like numbers, text,
and more complex data structures.
Variables are used to store and manipulate data within your game, and this data can be retrieved whenever
you'd like.
Variables have an identifier, such as a name and a value associated with that identifier.
A great analogy for a variable is a mailbox.
You can think of a mailbox like a variable because it has an address or an identifier, and that mailbox
can hold different items, which would be your values.
Just like you can put letters, packages, and other items into a mailbox and retrieve them when needed.
In programming, you can store various types of data in a variable and access that data later on by
referring to that variable's name.
So to create a variable, the first thing we need to do is type out the name or identifier for the variable.
In this case, we can call our first variable something like my first var.
This is going to be acting as our identifier or name for the variable, and then to store a value or
associate a value with this identifier, we need to use one equal sign.
The equal sign is known as an assignment operator.
In Lua, we are associating or assigning a value with the identifier.
And as an example, what I'm going to store in this variable is going to be the number like ten.
So now here we are.
We have our variable called my first var and it stores the value of ten somewhere in our computer's
memory.
Now we can store a lot more than just numbers inside of our variables.
By default, Lua has seven basic data types that you can store inside of your variables.
These data types are numbers, strings, booleans, functions, tables, threads, and nil.
Now again, numbers can be anything from negative to positive, whole or decimal.
Any whole numbers.
We like to call them integers.
So as an example ten.
Here would be an integer.
And any numbers that have decimals are called floating point numbers or float for short.
So for example, if I put a dot here and then put a zero, this would now be considered a floating point
number.
The reason we call them floating point numbers is because the decimal point that describes the fraction
of the number is able to float around or change to represent a different number.
For example, if we had a number like 3.14, this could be changed into something like 31.4, or we
could do 0.31 for the decimal point can shift or float around.
Now strings like we talked about previously are any text between quotation marks.
We can use strings to do things like share messages to players, make announcements in our game, run
a countdown timer, and much more.
So whenever you want to work with text in your scripts, you need to place that text between quotation
marks to denote it as a string.
Data type.
So if I wanted to say something like hello, I would put my quotation marks here and type in hello.
And now my variable is storing a string data type.
Next is a boolean.
And a boolean is a very simple data type that represents two possible values, either true or false.
Boolean values.
Let our computers make decisions based on a condition.
For example, we can check if a player has VIP and if they do have VIP, we can give them some extra
goodies associated with that VIP.
So in my first var, I can store a value like false by just typing out false or typing out something
like true.
That is our boolean data type.
Now functions which we will go over later are stored in variables as well.
They have an identifier and you can refer to this identifier and basically call the function to have
it execute.
Code functions are simply blocks of code that perform a specific task.
And as I said, we will take a look at them more closely later.
Now a table is another data type and they are created by using curly brackets.
So up here if I put two curly brackets I have now created a table data type and this table is currently
empty.
Tables can be used to store many different values and they can even represent different data structures.
We will be going over tables later in the course, so I wouldn't worry too much about them for now.
The next data type is known as a thread, and a thread is created by something in Lua called a coroutine.
Once more.
We do not need to worry about this data type for now either.
The last, but definitely not the least data type is nil.
This is a special value that represents the absence of a value or the concept of nothing in programming,
so we use Nil to indicate that a variable does not currently have any assigned value.
So up here, if I set my first var equal to nil, I'm basically saying that my first var has no.
Value at all.
It hasn't been initialized with anything, so nil represents the absence of a value.
Now the next thing I'm going to quickly note is that you may be wondering why all of this text inside
of my scripts is grayed out.
And this is because these are comments, which is for me and other people to read and not the computer.
If you want to make a comment, you can do so very easily by just putting two dashes before any text.
So right here, when I type out something like hello there, the interpreter is trying to read this
as code.
And because this is not code, we're getting an error here.
But if I put two dashes before it now I have created a comment and this is just there to help you make
notes inside of your code.
Maybe there's an area that might be a little confusing and you want to clarify.
You put comments in there to make it easier to understand.
We are also able to make multi-line comments as shown right here.
And the way to make a multi-line comment is you first go, you put two dashes and then you go ahead
and put two of these brackets.
And this creates a multi-line comment.
We can also do the same thing with strings.
So if you don't want to use quotations you can use those two brackets.
And now we can create a string that expands over multiple different lines.
So this is a multi-line string.
So if we try to do this with quotation marks it's obviously not going to work.
As you can see we're getting a bunch of red underlines here because we have to do it with two brackets
for multi-line strings.
Now another thing I want to mention with variables are the names that we have given them.
You want to make sure that the names you give to your variables are descriptive, so you can understand
what the value inside of them is supposed to represent.
Traditionally, we name variables using the camel case naming convention.
This means that the first letter in the first word of your variable is lowercase, while every other
word after your first word starts with an uppercase letter.
This is the camel case naming convention.
Now, you can also not name your variables starting with numbers.
So if I try to put a one here, we're going to get an error because we cannot name our variables starting
with numbers.
We also cannot name our variables with the keywords that are reserved in Lua.
So for example, one keyword that is reserved in Lua would be something like if and.
As you can see, we're getting a big red error here because we cannot override this default keyword
in Lua as a variable.
If we made it into something like if like or something like that, then it works just fine.
But we cannot override the defaults.
Uh, but we cannot override the default keywords that are in the Lua language.
Now, you might be wondering why do we name our variables with this weird camel case?
Well, the reason we do this is to differentiate things from being a variable, a class, or whatever
else camel case is a commonly used naming convention in Lua programming, and some other commonly used
naming conventions is something known as snake case.
So if I go down here, we have different other naming conventions that are commonly used.
So here is our camel case my first var.
There's also something known as snake case which is popularly used in the Python programming languages.
And then we also have something known as Pascal case.
So you can see the differences between these three different naming conventions.
Pascal case has the first letter capitalized.
Snake case has them all lowercase with an underscore separating them, and then camel case starts with
a lowercase and continues on with capitalized letters.
We typically use Pascal case when we give variable names for classes or something called enums.
Snake case basically has the same principle as camel case.
We use them for variables and stuff like that, but most commonly you'll be using camel case in Lua.
We can also use snake case in another way by capitalizing all of the letters in snake case.
So if we do something like this, my first var, then this variable name right here is actually representing
a constant or a value that should never change during the runtime of your game.
So if you want to define some particular constants in your game or values you never want to change,
then you would want to do it by using capitals and then underscores to separate the different words
from each other.
I'm also going to scroll up here and give you another look at some valid and invalid variable names.
So again, we cannot name our variables starting with numbers.
We cannot use any of the reserved keywords in Lua.
And we can also not use special characters that are reserved in Lua as well.
We can't use exclamation marks, we can't use multiplication symbols or the and symbol, stuff like
that.
These are invalid variable names that are not going to work, but you can go ahead and use these variable
names.
These work just fine.
Now one last thing I want to mention is that variable names are case sensitive in Lua, meaning that
I could have two variables with the exact same words like these ones here below.
But I can have different capitalization between these two names.
And because there's different capitalization, that actually means these two variable names are completely
different.
These two variables would reference different places in memory, and they're not the same.
So you need to keep in mind that Lua is case sensitive.
So you got to make sure that when you are typing out or referencing different variables or properties,
that the case matches perfectly.
Otherwise you're going to run into some problems.
The most important thing to understand from this lecture is to be consistent with your code.
Ensure that you are following a naming convention consistently and you don't want to do something like
type out variable names with multiple different naming conventions.
So as an example, if you were typing out a bunch of variable names for things in your scripts, don't
use multiple different types of naming conventions and stuff like that, because that's extremely ugly
and it makes your code difficult to read.
So as you can see here, if we did something like this and we try to read all the different variable
names, it's very inconsistent.
None of them are using the same naming convention.
Your eyes have to switch around between how the words are placed together.
It's just it's awful.
It's ugly.
Don't ever do this.
Another thing that I've commonly seen done when people are creating their variables is they omit or
remove the spaces between the assignment operator and the value they are assigning to their variable.
So I've seen people do something like this again, don't do this.
This is very ugly.
It's like you were writing an essay, but you decided to leave out all the spaces between the words.
It's ugly, it's hard to read and it just looks dumb.
So don't ever do that.
Okay, so let's go ahead and start practicing using some variables.
So I'm going to create a few variables here.
And you can go ahead and follow along with me I'm going to call this variable something like quote of
the day.
Again we're using the camel case naming convention.
We're going to use our assignment operator.
And we're going to assign a string value to this.
And the quote of the day can be something like, don't be a lazy coder.
There we go.
So now we have a variable.
And this variable is storing a string data type.
We can go ahead and create another variable.
I'm just going to call this one a Boolean example.
And inside of here we're just going to store a true value.
So there we go.
We have a variable storing a boolean data type.
We're going to go ahead and create another variable.
I'm going to call this one number example.
And inside of here we can store a floating point number like 1.923.
And then we can go ahead and create another variable.
I'm going to call this one nil example.
And we're just going to set this equal to nil.
So there we go.
Now the very cool thing is we can reference these different variables by referring to their names or
their identifiers.
So what we're going to do is we're going to use our print function and we're going to pass the name
of these different variables.
So for example here I can type out quote of the day.
And then I can hit tab to autofill that right there.
And then we can use the print function again.
And we can print out our Boolean example.
We can print out our number example and we can print out our nil example.
Now what this is going to do is it's not going to print out the name of the variable, but instead it's
going to look at this variable.
It's going to find whatever is stored in memory, which in this case would be our string or this one
would be the boolean, whatever.
It would take that value and it's going to print it out into our output or console.
If you do not have your output or console open, make sure you head up to the view tab and enable the
output button, which is right here.
So now we can go ahead and see these values get printed out in our console by going to our test tab,
hitting this little drop down arrow here and pressing run.
And now, as you can see, we get the different values printed inside of our console.
So we get our message.
Don't be a lazy coder.
We get true printed out, we get our number printed out, and then we get nil printed out so we can
go ahead and end the test.
And one more thing I would like to show you is that we can also update the values inside of our variables.
So when we instantiate or create this variable, it's not going to be stuck with a string forever.
We can easily change the value stored inside of this variable to whatever we'd like.
So for example, I can refer to quote of the day.
And then to override the value inside of here, I'm just going to use the assignment operator once more.
And inside of here I can change it to be something like a number.
So we can put in something like 100.
And now when our game is going to execute, it should print out 100 here and not our string because
we first create the string and then later it gets overridden with the value of 100.
So if we go back to the test tab and hit run.
As you can see, our message is gone and now we have 100 printed into the console.
This perfectly demonstrates how Lua is a serially executed programming language.
What does that mean?
Well, it means that Lua code is executed line by line, starting at the top and going down each line
until it reaches the bottom of your script.
So when our game is run, the interpreter goes into our script and it starts executing code.
It goes to the first line of code, which would be here.
It would be like, okay, I need to create a variable with this value inside of it.
So it stores that information in memory.
It goes to the next line oh, I got to do the same thing.
Creates another variable, stores that value in memory and it keeps going down.
And then eventually it's going to hit this line right here.
And it's going to see, oh we're referring to a variable that we've already created previously.
And we want to override the value stored inside of that variable with this new value.
So it does that.
It removes our string and replaces it with this number.
And then finally when we get down to the different print functions it prints out those values inside
of our console.
In this case that would be 100 true 1.923 and then nil.
So congratulations, you've reached the end of the first Lua basics lecture.
This was a lot of information to digest, but you're going to be using variables all of the time, so
it's very important you understand and get comfortable using them.
See you in the next lecture.